export default {
  install(Vue) {
    // const server = '' //外网+外网ip
    const server = 'https://www.unicorn-500.com/api' //本地ip
    Vue.prototype.$server = `${server}`
    Date.prototype.format = function (format) {
      if (this == '') return ''
      let args = {
        'M+': this.getMonth() + 1,
        'd+': this.getDate(),
        'h+': this.getHours(),
        'm+': this.getMinutes(),
        's+': this.getSeconds(),
        'q+': Math.floor((this.getMonth() + 3) / 3), //quarter
        S: this.getMilliseconds()
      }
      if (/(y+)/.test(format))
        format = format.replace(
          RegExp.$1,
          (this.getFullYear() + '').substr(4 - RegExp.$1.length)
        )
      for (let i in args) {
        let n = args[i]
        if (new RegExp('(' + i + ')').test(format))
          format = format.replace(
            RegExp.$1,
            RegExp.$1.length == 1 ? n : ('00' + n).substr(('' + n).length)
          )
      }
      return format
    }

    function urlPost(url, param, handler, method,) {
      console.log(method, param, 'methodmethod');

      wx.showLoading({
        title: '加载中'
      })
      var token = wx.getStorageSync('token')
      uni
        .request({
          url: `${server}${url}`,
          method,
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Authori-Zation': `Bearer ${token}`
          },
          'Content-Type': 'application/x-www-form-urlencoded',
          data: param
        })
        .then(res => {
          wx.hideLoading()
          let parse = res[1]?.data
          handler(parse?.data || {})
          if (parse.status != 200) {
            global.showToast(parse.msg)
          }
        })
        .catch(err => {
          handler(null)
          global.showToast('系统繁忙，请稍后再试')
        })
    }

    global.getOpenid = param =>
      new Promise((resolve, reject) => {
        urlPost('/wechat/mp_auth', param, res => {
          if (res == null) reject(null)
          else resolve(res)
        }, 'post')
      });
    //微信登录
    (global.wxlogin = param =>
      new Promise((resolve, reject) => {
        urlPost('/wechat/mp_auth', param, res => {
          if (res == null) reject(null)
          else resolve(res)
        }, 'POST')
      })),
      //获取手机号
      (global.miniGetPhone = param =>
        new Promise((resolve, reject) => {
          urlPost('/login/mobile', param, res => {
            if (res == null) reject(null)
            else resolve(res)
          }, 'post')
        })),
      //获取个人信息
      (global.getUser = param =>
        new Promise((resolve, reject) => {
          urlPost('/user', param, res => {
            if (res == null) reject(null)
            else resolve(res)
          }, 'get')
        })),
      (Vue.prototype.$api = {
        //修改用户昵称头像接口
        editUser: param =>
          new Promise((resolve, reject) => {
            urlPost('/user/edit', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }, 'post'),
        //修改用户手机号
        updatePhone: param =>
          new Promise((resolve, reject) => {
            urlPost('/user/updatePhone', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            }, 'post')
          }),
        //获取验证码
        getMobileCode: param =>
          new Promise((resolve, reject) => {
            urlPost('/register/verify', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            }, 'post')
          }),
        //获取验证码
        VerifyCode: param =>
          new Promise((resolve, reject) => {
            urlPost('/verify_code', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            }, 'get')
          }),
        //项目列表
        getProjectList: param =>
          new Promise((resolve, reject) => {
            urlPost('/xiangmu', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //企业申请入驻查询
        getComponyList: param =>
          new Promise((resolve, reject) => {
            urlPost('/user/shenqing_list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //企业申请入驻
        componyEnter: param =>
          new Promise((resolve, reject) => {
            urlPost('/user/shenqing', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            }, 'post')
          }),
        //企业申请入驻
        componyShenqing: param =>
          new Promise((resolve, reject) => {
            urlPost('/user/mod_shenqing', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            }, 'post')
          }),
        //文章分类列表
        getArticleCategory: param =>
          new Promise((resolve, reject) => {
            urlPost('/article/category/list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //文章分类列表
        getArticleList: param =>
          new Promise((resolve, reject) => {
            urlPost('/article/list/0', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //文章详情
        getArticleDetails: param =>
          new Promise((resolve, reject) => {
            urlPost(`/article/details/${param.id}`, param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //专题分类列表
        getZhuatiType: param =>
          new Promise((resolve, reject) => {
            urlPost('/zhuanti/type/list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //专题列表
        getZhuati: param =>
          new Promise((resolve, reject) => {
            urlPost('/zhuanti/list/0', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //专题详情
        getZhuatiDetails: param =>
          new Promise((resolve, reject) => {
            urlPost(`/zhuanti/details/${param.id}`, param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //用户添加收藏接口
        addCollect: param =>
          new Promise((resolve, reject) => {
            urlPost('/collect/add', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            }, 'post')
          },),
        //用户取消收藏接口
        delCollect: param =>
          new Promise((resolve, reject) => {
            urlPost('/collect/del', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            }, 'post')
          },),
        //我的收藏列表
        getMyCollect: param =>
          new Promise((resolve, reject) => {
            urlPost('/collect/user', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }, 'post'),
        //工程专题页列表
        getGongcheng: param =>
          new Promise((resolve, reject) => {
            urlPost('/gongcheng_config', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //大会分类查询接口
        getDahuiType: param =>
          new Promise((resolve, reject) => {
            urlPost('/dahui/type/list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //大会列表查询接口
        getDahui: param =>
          new Promise((resolve, reject) => {
            urlPost('/dahui/list/0', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //大会详情查询接口
        getDahuiDetails: param =>
          new Promise((resolve, reject) => {
            urlPost(`/dahui/details/${param.id}`, param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),

        //孵化器列表查询接口 
        getFuhuaqi: param =>
          new Promise((resolve, reject) => {
            urlPost('/fuhuaqi/list/0', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //读书会列表查询接口 
        getReading: param =>
          new Promise((resolve, reject) => {
            urlPost('/admin/crud/dushuhui', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //读书会分类列表 
        getReadingType: param =>
          new Promise((resolve, reject) => {
            urlPost('/dushuhui/type/list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //小程序首页接口 
        getIndex: param =>
          new Promise((resolve, reject) => {
            urlPost('/shouye', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //小程序新商学接口 
        getXinshangxue: param =>
          new Promise((resolve, reject) => {
            urlPost('/xinshangxue', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //企业特训团

        gongchengConfig: param =>
          new Promise((resolve, reject) => {
            urlPost('/gongcheng_config', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //项目分类列表 
        getProjectType: param =>
          new Promise((resolve, reject) => {
            urlPost('/xiangmu/type/list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //项目列表查询接口 
        getProject: param =>
          new Promise((resolve, reject) => {
            urlPost('/xiangmu/list/0', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //项目详情查询接口 
        getProjectDetails: param =>
          new Promise((resolve, reject) => {
            urlPost(`/xiangmu/details/${param.id}`, param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //企业列表查询接口 
        getCompanyList: param =>
          new Promise((resolve, reject) => {
            urlPost(`/qiye/list/${param.country_id}`, { ...param }, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //企业分类列表 
        getCompanyType: param =>
          new Promise((resolve, reject) => {
            urlPost('/qiye/type/list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //企业详情列表 
        getCompanyDetails: param =>
          new Promise((resolve, reject) => {
            urlPost(`/qiye/details/${param.id}`, param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //企业总裁特训
        getCompanyTraning: param =>
          new Promise((resolve, reject) => {
            urlPost('/qiye_texuntuan', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //投资机构列表查询接口 
        getTzjg: param =>
          new Promise((resolve, reject) => {
            urlPost('/tzjg/list/0', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //投资机构分类列表
        getTzjgType: param =>
          new Promise((resolve, reject) => {
            urlPost('/tzjg/type/list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //项目发布
        getUserPublish: param =>
          new Promise((resolve, reject) => {
            urlPost('/user/fabu_list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //项目发布
        operaUserPublish: param =>
          new Promise((resolve, reject) => {
            urlPost('/user/fabu', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            }, 'post')
          }),
        //项目发布
        operaModUserPublish: param =>
          new Promise((resolve, reject) => {
            urlPost('/user/mod_fabu', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            }, 'post')
          }),
        //项目发布
        getUserShenQing: param =>
          new Promise((resolve, reject) => {
            urlPost('/user/shenqing_list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //项目发布
        getHuiyuanQuanyi: param =>
          new Promise((resolve, reject) => {
            urlPost('/huiyuan_quanyi', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //项目发布
        getCountryArea: param =>
          new Promise((resolve, reject) => {
            urlPost('/country_area', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
        //独角兽榜单
        getCompany: param =>
          new Promise((resolve, reject) => {
            urlPost('/bangdan/type/list', param, res => {
              if (res == null) reject(null)
              else resolve(res)
            })
          }),
      })
  }
}
